/*
 * A CORBA Generic Server
 * Part of the GICOM project
 */
package gicom;

import java.io.Externalizable;
import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;

public abstract class PersistentObject extends GenericObject implements Externalizable {
    
    public PersistentObject () {
    }
    
    public PersistentObject (String id) {
        super(id);
    }
    
    public abstract void doReload (ORB orb, POA poa) throws Exception;
    
    /**
     * Save the object
     *
     */
    public void save () throws Exception {
        GenericServer.defaultGenericServer.getGenericManager().saveObject(this.getId());
    }
    
    void reload(final ORB orb, final POA poa) throws Exception {
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    PersistentObject.this.doReload(orb, poa);
                } catch (Exception ex) {
                    this.interrupt();
                }
            }
        };
        t.start();
        synchronized (GenericServer.defaultGenericServer.getGenericManager()) {
            while (t.isAlive()) GenericServer.defaultGenericServer.getGenericManager().wait(100);
        }
        if (t.isInterrupted()) throw new Exception("Reload failed");
    }
    
}
